package com.app.team.liquid;

//============================================================================
//Name        : ThreadView.java
//Author      : Kyle Bartush
//Version     : 0.1
//Description : Activity that shows all the posts of a particular thread.
//				Currently uses web views for the content and a text view for
//				the post title.
//============================================================================

import java.io.IOException;
import java.util.List;
import java.util.ListIterator;

import org.jsoup.nodes.Element;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class ThreadView extends Activity implements OnClickListener {

	static float TITLE_TEXT_DPIXELS = 10f;
	LinearLayout posts;
	int titlePixels;
	int titleHeight;
	int paddingSides;
	String currentPage;
	String threadUrl, threadAuthor, threadTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.thread_view);
		getData();
		this.setTitle(threadTitle);

		TLParse parser = new TLParse(this.getApplication());
		try {
			parser.setHtmlByUrl(threadUrl + "&currentpage=" + currentPage);
		} catch (IOException e) {
			e.printStackTrace();
		}

		List<ForumPost> postsList;
		posts = (LinearLayout) findViewById(R.id.postsContainer);
		DisplayMetrics metrics = this.getResources().getDisplayMetrics();
		titlePixels = (int) (metrics.density * TITLE_TEXT_DPIXELS + 0.5f);
		titleHeight = (int) (metrics.density * 35 + 0.5f);
		paddingSides = (int) (metrics.density * 10 + 0.5f);

		parser.parsePosts();
		postsList = parser.getPostsList();

		ListIterator<ForumPost> itr = (ListIterator<ForumPost>) postsList
				.listIterator();
		while (itr.hasNext()) {
			setPostTitle(itr.next());
			setPostContent(itr.previous());
			itr.next();
		}
		
		setNavButtons();

	}

	public void onClick(View view) {
		int currentId = view.getId();		
		if(currentId == 0){
			currentPage = Integer.toString((Integer.parseInt(currentPage) - 1));
		}
		if(currentId == 1){
			currentPage = Integer.toString((Integer.parseInt(currentPage) + 1));
		}		
		
		Bundle urlBun = new Bundle();
		urlBun.putString("url", threadUrl);
		urlBun.putString("title", threadTitle);
		urlBun.putString("author", threadAuthor);
		urlBun.putString("page", currentPage);
		Intent i = new Intent(ThreadView.this, ThreadView.class);
		i.putExtras(urlBun);
		startActivity(i);
		this.finish();
		
	}

	private void setPostTitle(ForumPost post) {
		TextView postTitle = new TextView(this);
		postTitle.setText(post.title);
		postTitle.setTextSize(titlePixels);
		postTitle.setBackgroundColor(this.getResources().getColor(
				R.color.post_title));
		postTitle.setTextColor(Color.BLACK);
		postTitle.setPadding(paddingSides, paddingSides, paddingSides,
				paddingSides);
		postTitle.setGravity(Gravity.CENTER_VERTICAL);
		posts.addView(postTitle);
	}

	private void setPostContent(ForumPost post) {

		WebView postContent = new WebView(this);
		postContent.loadData(post.content, "text/html", "utf-8");
		postContent.setPadding(paddingSides * 2, paddingSides * 2,
				paddingSides * 2, paddingSides * 2);
		posts.addView(postContent);
	}

	private void getData() {
		Bundle inBundle = getIntent().getExtras();
		threadUrl = inBundle.getString("url");
		threadTitle = inBundle.getString("title");
		threadAuthor = inBundle.getString("author");
		currentPage = inBundle.getString("page");
	}

	private void setNavButtons() {
		Button next, previous;
		LinearLayout layout = new LinearLayout(this);
		next = new Button(this);
		previous = new Button(this);
		next.setText("Next");
		previous.setText("Previous");
		previous.setId(0);
		next.setId(1);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.addView(previous);
		layout.addView(next);
		next.setOnClickListener(this);
		previous.setOnClickListener(this);
		posts.addView(layout);
	}

}
